//
//  TowBarCodeViewController.m
//  BaseTools
//
//  Created by 李挺哲 on 15/8/12.
//  Copyright (c) 2015年 ltz. All rights reserved.
//

#import "TowBarCodeViewController.h"
#import "QRViewController.h"
#import "UIImage+UIImage_Helper.h"
@interface TowBarCodeViewController()
@property (strong, nonatomic) IBOutlet UILabel *resultLabel;
@property (strong, nonatomic) NSString *code;
@property (assign, nonatomic) CGFloat  currentBrightness;
@property (weak, nonatomic)   IBOutlet UIView         *broadcastView;
@property (weak, nonatomic)   IBOutlet UIScrollView   *scrollView;
@property (strong, nonatomic)          UILabel        *adContentLabel;
@property (strong, nonatomic)          NSTimer        *scrollTimer;
@property (weak, nonatomic)   IBOutlet UIButton       *accessButton;
@property (weak, nonatomic)   IBOutlet UIImageView    *barCodeImageView;
@property (weak, nonatomic)   IBOutlet UIImageView    *qrCodeImageView;
@property (strong, nonatomic)          UIView         *barCodeContentView;
@property (strong, nonatomic)          UIView         *qrCodeContentView;
@property (strong, nonatomic)          UIImageView    *barCodeSizeImageView;
@property (strong, nonatomic)          UIImageView    *qrCodeSizeImageView;
@property (strong, nonatomic)          UILabel        *barCodeSizeLabel;

@end
@implementation TowBarCodeViewController

-(void)viewDidLoad{


    [super viewDidLoad];
    [self initNavigationTitle:@"二维码" barTintColor:0x1E86CB titleColor:0xFFFFFF isTranslucent:NO isHidden:NO];
    [self showNavigationLeftButtonTitle:@"返回" tintColor:0xFFFFFF];
    /**
     *  设置广播栏
     */
    // 禁止手动滚动
    _scrollView.scrollEnabled = NO;
    _adContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 34)];
    [_scrollView addSubview:_adContentLabel];
    _scrollView.contentSize = _adContentLabel.frame.size;
    _adContentLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
    _adContentLabel.text = @"尊敬的用户，请扫描二维码成为会员，享受更多优惠。";
    _scrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(scroll) userInfo:nil repeats:YES];
    // 添加事件
    _broadcastView.userInteractionEnabled = YES;
    [_broadcastView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)]];
    /**
     *  设置条形码和二维码
     */
    _qrCodeContentView = [[UIImageView alloc] init];
    _qrCodeImageView.userInteractionEnabled = YES;
    _barCodeContentView = [[UIImageView alloc] init];
    _barCodeImageView.userInteractionEnabled = YES;
    [_qrCodeImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapQRCodeBigger)]];
    [_barCodeImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBarCodeBigger)]];
    
    
 
    _code=@"www.baidu.com";
    _barCodeImageView.image=[UIImage generateBarCode:_code width:_barCodeImageView.frame.size.width height:_barCodeImageView.frame.size.height];
    _qrCodeImageView.image=[UIImage generateQRCode:_code width:_qrCodeImageView.frame.size.width height:_qrCodeImageView.frame.size.height];

}

#pragma mark - 循环滚动
- (void)scroll {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    
    [_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x + 10, 0) animated:NO];
    
    [UIView commitAnimations];
    
    if (_scrollView.contentOffset.x > _adContentLabel.frame.size.width) {
        _scrollView.contentOffset = CGPointMake(-_adContentLabel.frame.size.width, 0);
    }
}


#pragma mark - 点击
- (IBAction)click:(id)sender {
    NSLog(@"click");
}


- (void)tapQRCodeBigger
{
    _currentBrightness = [UIScreen mainScreen].brightness;
    
    // 调整屏幕亮度
    [[UIScreen mainScreen] setBrightness:1.0];
    
    // 隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    
    // 创建全屏背景图
    _qrCodeContentView.frame = [UIScreen mainScreen].bounds;
    _qrCodeContentView.backgroundColor = [UIColor whiteColor];
    
    // 创建image view
    _qrCodeSizeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _qrCodeImageView.frame.size.width, _qrCodeImageView.frame.size.height)];
    _qrCodeSizeImageView.center = CGPointMake(_qrCodeContentView.frame.size.width / 2.0, _qrCodeContentView.frame.size.height / 2.0);
    _qrCodeSizeImageView.image = [UIImage imageWithCIImage:[_qrCodeImageView.image CIImage] scale:1.0 orientation:UIImageOrientationUp];
    [_qrCodeContentView addSubview:_qrCodeSizeImageView];
    
    [self.view addSubview:_qrCodeContentView];
    [self.view bringSubviewToFront:_qrCodeContentView];
    
    _qrCodeContentView.userInteractionEnabled = YES;
    [_qrCodeContentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapQRCodeSmaller)]];
}

- (void)tapQRCodeSmaller
{
    self.navigationController.navigationBarHidden = NO;
    [_qrCodeContentView removeFromSuperview];
    [[UIScreen mainScreen] setBrightness:_currentBrightness];
}

- (void)tapBarCodeBigger
{
    _currentBrightness = [UIScreen mainScreen].brightness;
    // 调整屏幕亮度
    [[UIScreen mainScreen] setBrightness:1.0];
    
    // 隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
    
    // 创建全屏背景图
    _barCodeContentView.frame = [UIScreen mainScreen].bounds;
    _barCodeContentView.backgroundColor = [UIColor whiteColor];
    
    // 创建image view
    _barCodeSizeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _barCodeImageView.frame.size.height, _barCodeImageView.frame.size.width)];
    _barCodeSizeImageView.center = CGPointMake(_barCodeContentView.frame.size.width / 2.0, _barCodeContentView.frame.size.height / 2.0);
    _barCodeSizeImageView.image = [UIImage imageWithCIImage:[_barCodeImageView.image CIImage] scale:1.0 orientation:UIImageOrientationRight];
    [_barCodeContentView addSubview:_barCodeSizeImageView];
    
    [self.view addSubview:_barCodeContentView];
    [self.view bringSubviewToFront:_barCodeContentView];
    
    _barCodeContentView.userInteractionEnabled = YES;
    [_barCodeContentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBarCodeSmaller)]];
}

- (void)tapBarCodeSmaller
{
    self.navigationController.navigationBarHidden = NO;
    [_barCodeContentView removeFromSuperview];
    [[UIScreen mainScreen] setBrightness:_currentBrightness];
}


- (IBAction)QRRecgoniseAction:(id)sender {
    if ([self validateCamera]) {
        
        [self performSegueWithIdentifier:@"toReg" sender:self];
        
    } else {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有摄像头或摄像头不可用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }

    }

- (BOOL)validateCamera {
    
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] &&
    [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    QRViewController * VC= segue.destinationViewController;
    VC.qrUrlBlock=^(NSString * url){
    
        _resultLabel.text=url;
    };
}
@end
